package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Goods;
import com.cskaoyan.bean.domain.GoodsExample;
import com.cskaoyan.bean.domain.Topic;
import com.cskaoyan.bean.domain.TopicExample;
import com.cskaoyan.bean.topic.TopicBatchDeleteBO;
import com.cskaoyan.bean.topic.TopicReadVO;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author:
 * @date:
 */
@Transactional
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BaseData topicList(String title, String subtitle, BaseParam baseParam) {
        TopicExample topicExample = new TopicExample();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (!MyUtils.isEmpty(title))
            criteria.andTitleLike("%" + title + "%");
        if (!MyUtils.isEmpty(subtitle))
            criteria.andSubtitleLike("%" + subtitle + "%");
        criteria.andDeletedEqualTo(false);
        topicExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo pageInfo = new PageInfo(topics);
        BaseData baseData = BaseData.getBack(baseParam.getLimit(), baseParam.getPage(), pageInfo.getPages(), (int) pageInfo.getTotal(), topics);
        return baseData;
    }

    @Override
    public TopicReadVO read(Integer id) {
        //获取topic对象
        Topic topic = topicMapper.selectByPrimaryKey(id);
        GoodsExample goodsExample = new GoodsExample();
        //获取topic对象里的goodsId数组
        Integer[] goods1 = topic.getGoods();
        List<Goods> goods = null;
        if (goods1.length > 0) {
            //将goodsId数组转换为list，并作为in的查询条件
            List<Integer> integers = Arrays.asList(goods1);
            goodsExample.createCriteria().andIdIn(integers);
            goods = goodsMapper.selectByExample(goodsExample);
        }else {
            goods = new ArrayList<>();
        }
        //给返回值类赋值
        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setGoodsList(goods);
        topicReadVO.setTopic(topic);
        return topicReadVO;
    }

    @Override
    public Topic topicUpdate(Topic topic) {
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andIdEqualTo(topic.getId());
        topicMapper.updateByExampleSelective(topic, topicExample);
        Topic topic1 = topicMapper.selectByPrimaryKey(topic.getId());
        return topic1;
    }

    @Override
    public int topicDelete(Topic topic) {
        TopicExample topicExample = new TopicExample();
        Topic topic1 = new Topic();
        topic1.setDeleted(true);
        topicExample.createCriteria().andIdEqualTo(topic.getId());
        int affectedRow = topicMapper.updateByExampleSelective(topic1, topicExample);
        return affectedRow;
    }

    @Override
    public int topicBatchDelete(TopicBatchDeleteBO batchDeleteBO) {
        Topic topic = new Topic();
        topic.setDeleted(true);
        for (int i = 0; i < batchDeleteBO.getIds().size(); i++) {
            TopicExample topicExample = new TopicExample();
            topicExample.createCriteria().andIdEqualTo(batchDeleteBO.getIds().get(i));
            topicMapper.updateByExampleSelective(topic, topicExample);
        }
        return 1;
    }

    @Override
    public Topic topicCreate(Topic topic) {
        topicMapper.insertSelective(topic);
        Topic topic1 = topicMapper.selectByPrimaryKey(topic.getId());
        return topic1;
    }

    @Override
    public List<Topic> topicsWithOutPage() {
        //主题列表
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        return topics;
    }
}
